<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<style>
			.datepicker-input{height: 34px; border: 1px solid #D3D3D3; background-color: #fff; border-radius: 2px; width: 250px; padding: 0px 4px;}
			table.datagrid-ftable{background: beige;}
		</style>
		<div class="super-theme-example">
			<div class="options-toolbar">
				<form id="order_sallReportDatagridQueryParams">
					<label>投放人:</label><input class="easyui-textbox" name="affiliate" value="" data-options="width:120,prompt:'投放人'">
					<select class="easyui-combobox" name="currency" style="width:120px;">
					    <option value="">全部币种</option>
					    <option value="TWD">台币</option>
					    <option value="HKD">港币</option>
					</select>
					<label>美元-新台币:</label><input class="easyui-textbox" name="twd$" value="30.511" data-options="width:120,prompt:'美元-新台币'">
					<label>美元-港元:</label><input class="easyui-textbox" name="hk$" value="7.8276" data-options="width:120,prompt:'美元-港币'">
					<label>美元-人民币:</label><input class="easyui-textbox" name="cny$" value="7.0353" data-options="width:120,prompt:'美元-人民币'">
					<input id="order_sallReport-datepicker" class="datepicker-input" />
					<a href="javascript:void(0)" class="easyui-linkbutton primary" data-options="iconCls:'fa fa-search'" onclick="order_sall_report.methods.initDatagrid();">查询</a>
				</form>
			</div>
			<table id="order_sallReport-datagrid"></table>
		</div>
		<script type="text/javascript">
			var order_sall_report = {
				methods : {
					initDatagridQueryParams: function(){
						var params = $('#order_sallReportDatagridQueryParams').serializeArray();
						var queryParams = {};
						$.each(params , function(index , param){
						     queryParams[param.name] = param.value;
						})
						var time = $('#order_sallReport-datepicker').val();
						queryParams.begainTime = timestamp(time.substring(0, 19));
						queryParams.endTime = timestamp(time.substring(22));
						return queryParams;
					},
					initTimeSelect: function(){
						$('#order_sallReport-datepicker').daterangepicker($.extend(true, {} , globalDaterangepickerOptions));
					},
					initDatagrid : function(){
						var datagridData = {"rows": [] , "footer": []} ; 
						requestServer(commonConfig.mgUrlPrefix() + '/admin/order/report/sale', 'GET', order_sall_report.methods.initDatagridQueryParams()).then((response) => {
							if (response.code == 1) {
								var twd$ = $('input[name="twd$"]').val().trim() - 0,
									hk$ = $('input[name="hk$"]').val().trim() - 0,
									cny$ = $('input[name="cny$"]').val().trim() - 0;
								var hkd_data = {
									orderCount: 0,
									orderPrice: 0,
									orderPriceRMB: 0,
									orderPriceUSD: 0,
									orderProductLists: 0,
									orderProductQuantites: 0,
									orderPurchasePrices: 0,
								}, twd_data = {
									orderCount: 0,
									orderPrice: 0,
									orderPriceRMB: 0,
									orderPriceUSD: 0,
									orderProductLists: 0,
									orderProductQuantites: 0,
									orderPurchasePrices: 0,
								};
								$.each(response.datas , function(index , _data){
									var end_data
									switch(_data.currency){
										case "HKD": 
											end_data = Object.assign(_data, {
												orderPriceUSD: _data.orderPrice / hk$,
												orderPriceRMB: _data.orderPrice / hk$ * cny$,
											})
											for(var i in hkd_data){
												hkd_data[i] += (end_data[i] - 0)
											}
											break;
										case "TWD": 
											end_data = Object.assign(_data, {
												orderPriceUSD: _data.orderPrice / twd$,
												orderPriceRMB: _data.orderPrice / twd$ * cny$,
											})
											for(var i in twd_data){
												twd_data[i] += (end_data[i] - 0)
											}
											break;
									}
									datagridData.rows.push(end_data);
								});
								datagridData.footer.push(Object.assign(hkd_data, {
									currency: "HKD",
									affiliate: "合计",
								}))
								datagridData.footer.push(Object.assign(twd_data, {
									currency: "TWD",
									affiliate: "合计",
								}))
                                $('#order_sallReport-datagrid').datagrid({
                                    data: datagridData,
									showFooter: true,
                                    pagination: false,
                                    columns:[[
										{field:'currency',title:'商城（货币单位）',width:120,align:'center',formatter:function(value,row,index){
											var scope = ''
											switch(value){
												case "HKD": scope = '香港站'; break;
												case "TWD": scope = '台湾站'; break;
												default: scope = ''; break;
											}
                                            return scope + '<br>（' + value + '）';
                                        }},
                                        {field:'affiliate',title:'投放人',width:100,align:'center',formatter:function(value,row,index){
                                            return value || '--';
                                        }},
                                        {field:'orderCount',title:'订单数量',width:100,align:'center'},
                                        {field:'orderProductLists',title:'SKU数量',width:100,align:'center'},
                                        {field:'orderProductQuantites',title:'联单数',width:100,align:'center'},
                                        {field:'orderPrice',title:'订单总金额（原币种）',width:140,align:'center'},
                                        {field:'orderPriceUSD',title:'订单总金额（USD）',width:140,align:'center',formatter:function(value,row,index){
											return value.toFixed(2) || '--';
                                        }},
                                        {field:'orderPriceRMB',title:'订单总金额（RMB）',width:140,align:'center',formatter:function(value,row,index){
                                            return value.toFixed(2) || '--';
                                        }},
                                        {field:'orderPurchasePrices',title:'采购金额（RMB）',width:120,align:'center',formatter:function(value,row,index){
                                            return value.toFixed(2) || '--';
                                        }},
                                        {field:'a',title:'采购成本（%）',width:100,align:'center', formatter:function(value,row,index){
                                            return row.orderPriceRMB ? (row.orderPurchasePrices / row.orderPriceRMB * 100).toFixed(2) : 0;
                                        }},
                                    ]],
                                    loadFilter: function(data){return data;}
                                });
							}else {
								$.messager.alert('提示',response.message,'error');
							}
						}).catch((error) => {
							console.log(error)
						});
					}
				},
				init : function(){
					order_sall_report.methods.initTimeSelect();
					order_sall_report.methods.initDatagrid();
				}
			}
			order_sall_report.init();
		</script>
	</body>
</html>

